package com.ricchieri.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.dao.interf.IEmailTemplateDao;
import com.ricchieri.filter.EmailTemplateFilter;
import com.ricchieri.model.EmailTemplate;
import com.ricchieri.util.StringUtils;

public class EmailTemplateDao extends GenericDao implements IEmailTemplateDao {
	private static final long serialVersionUID = 3602749068868710598L;

	@Override
	@SuppressWarnings("unchecked")
	public List<EmailTemplate> getAllEmailTemplate(EmailTemplateFilter filter) {
		Criteria criteria = criteriaAllEmailTemplate(filter);
		
		return criteria.list();
	}

	@Override
	public void updateDefault(int templateType) {
		Query query = getSessionFactory().getCurrentSession().createQuery("UPDATE EmailTemplate e SET isDefault = 0  WHERE e.emailToSendType.id=:templateType");
		query.setParameter("templateType", templateType);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EmailTemplate> getAllEmailTemplatePaginatedByFilter(
			EmailTemplateFilter emailTypeToSend, int startRecord) {
		Criteria criteria = criteriaAllEmailTemplate(emailTypeToSend);
		criteria.setMaxResults(BusinessSettings.ROW_MAX_RESULT);
		criteria.setFirstResult(startRecord);
		
		return criteria.list();
	}

	@Override
	public int getAllEmailTemplatePaginatedCountByFilter(EmailTemplateFilter emailTypeToSend) {
		Criteria criteria = criteriaAllEmailTemplate(emailTypeToSend);
		Long result = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
		return new Integer(result.intValue());
	}
	
	private Criteria criteriaAllEmailTemplate(EmailTemplateFilter emailTypeToSend) {
		Criteria criteria = super.criteriaAllBasic(emailTypeToSend, EmailTemplate.class);
		
		if (!StringUtils.isEmpty(emailTypeToSend.getTitle())) {
			criteria.add(Restrictions.like(getTableName().concat(".title"), "%".concat(emailTypeToSend.getTitle()).concat("%")));
		}
		
		if (emailTypeToSend.getFindOnlyIsDefault()) {
			criteria.add(Restrictions.eq(getTableName().concat(".isDefault"), emailTypeToSend.getIsDefault()));
		}
		
		if (!StringUtils.isEmpty(emailTypeToSend.getEmailTemplateType())) {
			criteria.createAlias(getTableName().concat(".emailToSendType"), "emailToSendType");
			criteria.add(Restrictions.eq("emailToSendType.id", emailTypeToSend.getEmailTemplateTypeId()));
		}
		
		if (!StringUtils.isEmpty(emailTypeToSend.getEmailTemplateTypeDescription())) {
			criteria.createAlias(getTableName().concat(".emailToSendType"), "emailToSendType");
			criteria.add(Restrictions.eq("emailToSendType.description", emailTypeToSend.getEmailTemplateTypeDescription()));
		}
		
		return criteria;
	}

	@Override
	@SuppressWarnings("unchecked")
	public EmailTemplate getEmailTemplateByFilter(
			EmailTemplateFilter emailTemplateFilter) {
		Criteria criteria = criteriaAllEmailTemplate(emailTemplateFilter);
		List<EmailTemplate> result = criteria.list();
		
		if(result != null && result.size() > 0) {
			return result.get(0);
		} else {
			return null;
		}
	}

	@Override
	protected String getTableName() {
		return "emailTemplate";
	}
	
}
